<template>
    <div class="app-container">
        <el-table :data="kpList">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="详情图片" align="center" prop="pic">
                <template slot-scope="scope">
                    <el-image v-for="(item, index) in scope.row" :src="item.picFirst" :key="index" alt="" weight="20"
                        style="width:60px; height: 60px;" :preview-src-list="srcList" @click="look(scope.row)">
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column fixed="right" align="center" label="操作" width="200">
                <template slot-scope="scope">
                    <el-button @click="updata(scope.row)" type="text" size="small" icon="el-icon-edit">修改</el-button>
                    <el-popconfirm title="是否确认删除所有图片？若删除单张详情图片，请在修改操作下进行" @confirm="handledelete(scope.row)">
                        <el-button slot="reference" type="text" size="small" icon="el-icon-delete">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!-- 修改详情图片对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body @close="closeDialog">
            <el-form ref="form" :model="form" :rules="rules" label-width="150px">
                <el-form-item label="详情图片" prop="picc">
                    <el-upload class="avatar-uploader" :action="url" accept=".jpg, .png" :headers="headers" multiple
                        list-type="picture" :file-list="fileList" :on-success="handlePreview" :on-remove="handleRemove"
                        name="fileName">
                        <el-button size="small" type="primary">点击上传详情图片</el-button>
                        <div slot="tip" class="el-upload__tip">详情图片不能低于两张</div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="xiugai">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { hslfyp, updateHYJD, selectOneHYJD, updateHYJDsahnchu } from "@/api/business/zhuangxiu.js";
import { getToken } from '@/utils/auth'
export default {
    data() {
        return {
            srcList: [],
            fileList: [],
            headers: {
                Authorization: "Bearer " + getToken(),
            },
            url: process.env.VUE_APP_BASE_API + "/upload/fileUpload",
            title: '',
            open: false,
            kpList: [],
            date: {
                typeid: this.$route.query.typeid,
            },
            form: {
                pId: '',
                picc: '',
            },
            date1: {
                pId: ''
            },
            rules: {}
        }
    },
    created() {
        this.getList();
    },
    methods: {
        closeDialog() {
            this.fileList = [];
            this.srcList = [];
        },
        getList() {
            hslfyp(this.date).then(response => {
                this.kpList = response.data.data.shuju;
                this.kpList[0].forEach(item => {
                    this.srcList.push(item.picFirst);
                })
            })
        },
        // 修改
        updata(row) {
            row.forEach(item => {
                row.pId = item.pId
            });
            this.form.pId = row.pId;
            this.open = true;
            selectOneHYJD(this.form).then(response => {
                this.form.picc = response.data.data.picc;
                response.data.data.picc.split(',').forEach(item => {
                    let Object = {
                        name: '',
                        url: ''
                    }
                    Object.name = item.slice(item.lastIndexOf('/') + 1);
                    Object.url = item;
                    this.fileList.push(Object);
                })
            })

        },
        xiugai() {
            if (this.fileList.length < 2) {
                this.$modal.msgError("详情图片不能低于两张!");
                return false;
            } else {
                updateHYJD(this.form).then(response => {
                    this.getList();
                    this.open = false;
                    this.fileList = [];
                    this.form = {
                        pId: '',
                        picc: '',
                    }
                })
            }

        },
        // 修改详情图上传
        handleRemove(file, fileList) {
            this.fileList = fileList;
            let List = [];
            this.fileList.forEach(item => {
                if (item.response) {
                    List.push(item.response.data)
                } else {
                    List.push(item.url)
                }
            });
            this.form.picc = List.join(',')
        },
        handlePreview(response, file, fileList) {
            this.fileList = fileList;
            let List = [];
            this.fileList.forEach(item => {
                if (item.response) {
                    List.push(item.response.data)
                } else {
                    List.push(item.url)
                }

            });
            this.form.picc = List.join(',');
        },
        cancel() {
            this.open = false;
        },
        // 删除
        handledelete(row) {
            row.forEach(item => {
                row.pId = item.pId
            });
            this.date1.pId = row.pId;
            updateHYJDsahnchu(this.date1).then(response => {
                this.$modal.msgSuccess(response.data.msg);
                this.getList();
            })
        },
        look(row) {
            row.forEach(item => {
                this.srcList.push(item.picFirst);
            })
        }
    }
}
</script>